package com.jcwx.game.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import com.jcwx.game.domain.HimiObject;
import com.jcwx.game.socket.codex.QueueMessage;

public class ClientMinaServerHanlder extends IoHandlerAdapter {

	// 当一个客端端连结到服务器后
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		// session.write("我来啦........");
		
		// 第一次模拟登录指令
		
		Short head=4;
		// [139160, , 1401182753250, 43a86799e7a74cacb50abe01066d73ce, 1, 0, wonder, 68, Windows]
		List<Object> body =new ArrayList<Object>();
		
		
		QueueMessage queueMessage = new QueueMessage(session, head, body);
		session.write(queueMessage);
		System.out.println("组装消息...");
		
		
	}

	// 当一个客户端关闭时
	@Override
	public void sessionClosed(IoSession session) {
		System.out.println("I'm Client &&  I closed!");
	}

	// 当服务器端发送的消息到达时:
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		// // 我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为 String:
		// String s = (String) message;
		// // Write the received data back to remote peer
		// System.out.println("服务器发来的收到消息: " + s);
		// // 测试将消息回送给客户端 session.write(s);

		HimiObject ho = (HimiObject) message;
		System.out.println(ho.getName());

	}

}
